package com.heima.article.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ApArticleService;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.common.dtos.ResponseResult;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {

    @Autowired
    private ApArticleMapper apArticleMapper;
    @Override
    public ResponseResult loadArticleList(ArticleHomeDto dto,Long type) {
        //1、参数校验
        if(dto.getSize()!=null){
            dto.setSize(10);
        }
        if(!type.equals(2) && !type.equals(1)){
            type = 2L;
        }
        //文章频道校验
        if(StringUtils.isEmpty(dto.getTag())) {
            dto.setTag("__all__");
        }
        //时间校验
        if (dto.getMaxBehotTime() == null) dto.setMaxBehotTime(new Date());
        if (dto.getMinBehotTime() == null) dto.setMinBehotTime(new Date());
        //2、调用mapper层接口
        List<ApArticle> apArticles = apArticleMapper.loadArticleList(dto, type);
        //3、返回数据
        return ResponseResult.okResult(apArticles);

    }

}
